//
//  CDSettingSecondCell.m
//  CuriosityDaily3
//
//  Created by donghao on 16/9/23.
//  Copyright © 2016年 theBestTeam. All rights reserved.
//

#import "CDSettingSecondCell.h"

#import "CDSettingPF.h"

@interface CDSettingSecondCell ()

@property(nonatomic,weak) UIButton *smallButton;
@property(nonatomic,weak) UIButton *normalButton;
@property(nonatomic,weak) UIButton *bigButton;
@property(nonatomic,weak) UILabel *label;

@property(nonatomic,weak) UIButton *selectedButton;

@end

@implementation CDSettingSecondCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

/**
 *  设置界面
 */
- (void)setupUI {
    
    // label
    UILabel *label = [UILabel labelWithText:@"字体大小" andTextColor:[UIColor darkGrayColor] andFontSize:15];
    label.textColor = [UIColor whiteColor];
    [self.contentView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(13);
        make.bottom.equalTo(self.contentView).offset(-8);
    }];
    
    // 3个按钮
    UIButton *smallButton = [self buttonWithTitle:@"小"];
    UIButton *normalButton = [self buttonWithTitle:@"中"];
    UIButton *bigButton = [self buttonWithTitle:@"大"];
    
    smallButton.tag = CDSetButtonTypeSmall;
    normalButton.tag = CDSetButtonTypeNormal;
    bigButton.tag = CDSetButtonTypeBig;
    
    [self.contentView addSubview:smallButton];
    [self.contentView addSubview:normalButton];
    [self.contentView addSubview:bigButton];
    
    [bigButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView).offset(-5);
        make.right.equalTo(self.contentView).offset(-15);
        make.width.height.mas_equalTo(20);
    }];
    [normalButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bigButton.mas_left).offset(-15);
        make.centerY.equalTo(bigButton);
        make.size.equalTo(bigButton);
    }];
    [smallButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(normalButton.mas_left).offset(-15);
        make.centerY.equalTo(bigButton);
        make.size.equalTo(bigButton);
    }];
    
    self.label = label;
    self.smallButton = smallButton;
    self.normalButton = normalButton;
    self.bigButton = bigButton;
    
    CGFloat fontSize = [CDSettingPF sharedSettingPF].fontSize.floatValue;
//    NSLog(@"%f",fontSize);
    if (fontSize == 14) {
        [self buttonClickWithType:self.smallButton];
    } else if (fontSize == 16) {
        [self buttonClickWithType:self.normalButton];
    } else {
        [self buttonClickWithType:self.bigButton];
    }
    
}

- (UIButton *)buttonWithTitle:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    button.titleLabel.font = [UIFont systemFontOfSize:10];
    
    [button setBackgroundColor:[UIColor clearColor]];
    
    [button setBackgroundImage:[UIImage imageNamed:@"photo_original_def"] forState:UIControlStateNormal];
    button.layer.cornerRadius = 10;
    button.clipsToBounds = YES;
    [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateHighlighted];
//    //photo_original_def
    [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(buttonClickWithType:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (void)buttonClickWithType:(UIButton *)sender {
    
    if (self.selectedButton.tag == sender.tag) {
        self.selectedButton.selected = YES;
        sender.selected = YES;
        return;
    }
    
    self.selectedButton.selected = NO;
    
    self.selectedButton = sender;
    
    CDSetButtonType type = sender.tag;
    
    sender.selected = YES;
    
    if ([self.delegate respondsToSelector:@selector(cell:textFontButtonClick:)]) {
        [self.delegate cell:self textFontButtonClick:type];
    }

}

@end
